//draws an arrow from point (ax, ay) to (bx, by)
//can specify arrowhead size and angle
function arrowTo(paper, ax, ay, bx, by) {
  var slope = (by-ay)/(bx-ax);
  var arrowheadSize = 15;
  var p = paper.path("M"+ax+" "+ay+"L"+bx+" "+by);
  p.attr("stroke-dasharray",". ")
  p.attr("stroke-opacity",0.7)
  var head1 = paper.path("M0 0L"+arrowheadSize+" 0");
  var rotate1 = 10;//arrowhead angle
  var rotate2 = 360-rotate1;
  if (bx >= ax || bx-ax == 0) {
    rotate1 += 180;
    rotate2 += 180;
  }
  head1.translate(bx, by);
  head1.rotate(Math.atan(slope)*(180/Math.PI)+rotate1, bx, by);
  head2 = head1.clone();
  head2.rotate(Math.atan(slope)*(180/Math.PI)+rotate2, bx, by);
}

//raphael needs spot for top left corner; this is for centering the rectangle
function drawRectCenter(paper, x, y, xdim, ydim) {
  return paper.rect(x-0.5*xdim, y-0.5*ydim, xdim, ydim);
}

//used for javascript in html file because '<' sign cannot be used in html file for less than
function lessthan(a, b) {
  if (a < b)
    return 1;
  else
    return 0;
}

//object to hold information about role boxes
function RoleBoxInfo(nm, xc, yc, xbox, ybox) {
  this.name = nm;
  this.x = xc;//center of rectangle
  this.y = yc;//center of rectangle
  this.xbox = xbox;
  this.ybox = ybox;
}

//infoarray holds RoleBoxInfo instances
function findRoleByName(name, infoarray) {
  for (i = 0; i < infoarray.length; i++) {
    if (infoarray[i].name == name)
      return infoarray[i];
  }
  return 0;
}

//rectangle coordinates are from center of rectangle; need to draw arrow from outside of box
//if destination is higher than start, then arrow goes from top of start to bottom of destination; else arrow goes from bottom of start to top of destination
//a is start box, b is end box
//up arrows are slightly to left of center, down arrows are slightly to right
function roleToRoleArrow(paper, a, b) {
  if (b.y < a.y) {
    arrowTo(paper, a.x-(1/6)*a.xbox, a.y-0.5*a.ybox, b.x-(1/6)*b.xbox, b.y+0.5*b.ybox);
  }
  else
    arrowTo(paper, a.x+(1/6)*a.xbox, a.y+0.5*a.ybox, b.x+(1/6)*b.xbox, b.y-0.5*b.ybox);
}